package next.view.support;

import java.util.ArrayList;
import java.util.List;

import next.ControllerResponsibilities.ApplicationControllerListener;
import next.CustomLayout.TitleWidget;
import next.Functionality.S_3R4_Activity;
import next.TemplateActivity.R;
import next.control.ApplicationController;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class CustomList extends S_3R4_Activity {

	private ListView list;
	private List<S_3R4_Object> listOfObjects = new ArrayList<S_3R4_Object>();
	private S_3R4_CustomAdapter adapter;

	private Button Voice, NFC, BarCode, Map;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.generic_list_custom_layout);
		
		//--Initialize title Widget
        TitleWidget titleWidget = (TitleWidget)findViewById(R.id.normal_activity_CV_TWI1);
        //--Call the methods to change the underlying widgets
        titleWidget.setTitle("Listing");
        titleWidget.setIcon(R.drawable.icon);
        titleWidget.showProgressBar();
        titleWidget.hideProgressBar();
        
		// --This is for Demo Demonstration
		for (int i = 0; i < 11; i++) {
			listOfObjects.add(new S_3R4_Object("Chain" + i, "Chain2" + i, i,
					i + 1000));
		}

		// --Assembling Widgets
		Voice = (Button) findViewById(R.id.generic_list_custom_BN1);
		NFC = (Button) findViewById(R.id.generic_list_custom_BN2);
		BarCode = (Button) findViewById(R.id.generic_list_custom_BN3);
		Map = (Button) findViewById(R.id.generic_list_custom_BN4);

		list = (ListView) findViewById(R.id.generic_list_custom_layoutLV1);

		// --Adding items to list
		adapter = new S_3R4_CustomAdapter(this, listOfObjects);

		list.setAdapter(adapter);

		list.setTextFilterEnabled(true);

		list.setOnItemClickListener(new OnItemClickListener() {
		    @Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				String s = "**";
				for (S_3R4_Object node : listOfObjects) {
					if (node.getA() == arg3) {
						s += node.getChain1() + "\n";
						s += node.getChain2() + "\n";
						s += node.getA() + "\n";
						s += node.getB() + "\n";
					}
				}

				Toast.makeText(getBaseContext(), "You clicked on " + s,
						Toast.LENGTH_LONG).show();

			}
		});

		// --Adding Events
		Voice.setOnClickListener(ApplicationController.getInstance());
		NFC.setOnClickListener(ApplicationController.getInstance());
		BarCode.setOnClickListener(ApplicationController.getInstance());
		Map.setOnClickListener(ApplicationController.getInstance());
		 
	}

    //--Using Voice commands for text	
	@Override
	public void onRestart() {
		if (!ApplicationController.filterWord.equalsIgnoreCase(""))
	 	{
	 		adapter.getFilter().filter(ApplicationController.filterWord);
	 		ApplicationController.filterWord="";
	 	}
		//--Verify if the profile button was clicked
	 	super.onRestart();
	 	
	}



}